package com.weasel.weixin.mp;

import com.weasel.weixin.commons.menu.MenuRequest;
import com.weasel.weixin.commons.menu.MenuResponse;
import com.weasel.weixin.commons.menu.bean.Menu;
import com.weasel.weixin.commons.menu.builder.*;
import com.weasel.weixin.mp.builder.MpConfigBuilder;
import com.weasel.weixin.mp.config.MpConfig;
import com.weasel.weixin.mp.jetty.JettyServletLauncher;
import com.weasel.weixin.mp.request.MpMenuRequest;
import com.weasel.weixin.mp.servlet.BindUserServlet;
import com.weasel.weixin.mp.servlet.WeixinPostEventXmlMessageServlet;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

/**被动接收事件消息推送测试(当用户关注/取消关注公众号，点击菜单等，微信会将这些事件发送到开发者服务器，开发者可以接收消息并作出相应处理)
 * 运行后，请打开微信，对公众号进行关注或才取消关注，微信服务器会请求本地服务器，本地服务器被动接收事件。
 * Created by Dylan on 2015/9/28.
 */
public class WeiXinPostEventXmlMessagekDemo {

    public static void main(String [] args) throws IOException, ExecutionException {

       createMenu();

        JettyServletLauncher.listen(8080)
                            .addServletWithMapping(new BindUserServlet(), "/bind_user")
                            .addServletWithMapping(new WeixinPostEventXmlMessageServlet(), "/*")
                            .launch();
    }


    public static void createMenu() throws IOException, ExecutionException {
        MpConfig config = MpConfigBuilder.fromXML("classpath:weixin-config.xml");
        MenuRequest request = new MpMenuRequest(config);

        Menu menu = MenuBuilders.create()
                                .button(new ViewButtonBuilder().name("首页")
                                                .url("http://www.jlw365.com")
                                )
                                .button(new CommonButtonBuilder().name("优惠")
                                                .button(new ViewButtonBuilder().name("双十二")
                                                                .url("http://www.jlw365.com")
                                                ).button(new ScancodePushButtonBuilder().name("分享")
                                                                .key("uhui_scancode")
                                                )
                                )
                                .button(new CommonButtonBuilder().name("我的")
                                                .button(new ClickButtonBuilder().name("积分")
                                                                .key("my_score")
                                                )
                                )
                                .build();

        MenuResponse response = request.create(menu);

        System.out.println(response);
    }

}
